package com.gupao.vip.netty;

import com.gupao.vip.netty.entity.SubscribeRequest;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

public class SubReqClientHandler extends ChannelInboundHandlerAdapter {
    public SubReqClientHandler(){

    }

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        System.out.println("Receive server response : [" + msg + "]");
    }

    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
        ctx.flush();
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        System.err.println(cause.getMessage());
        ctx.close();
    }

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        for(int i=0; i<10; i++){
            ctx.write(createRequest(i));
        }
        ctx.flush();
    }

    private SubscribeRequest createRequest(int i){
        SubscribeRequest entity = new SubscribeRequest();
        entity.setSubReqID(i);
        entity.setAddress("南京市江宁区方山国家地质公园");
        entity.setPhoneNumber("18629043508");
        entity.setProductName("Netty 权威指南");
        entity.setUserName("majingang");
        return entity;
    }
}
